﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Enterprise.Dal
{
    /// <summary>
    /// 分页类
    /// </summary>
    public class AspNetPagerModel
    {
        private int currentPageIndex = 1;


        /// <summary>
        /// 当前页(默认1)
        /// </summary>
        public int CurrentPageIndex
        {
            get { return currentPageIndex; }
            set { currentPageIndex = value; }
        }

        private int pageSize = 10;

        /// <summary>
        /// 页大小(默认10)
        /// </summary>
        public int PageSize
        {
            get { return pageSize; }
            set { pageSize = value; }
        }

        private object dataSource;

        /// <summary>
        /// 数据源
        /// </summary>
        public object DataSource
        {
            get { return dataSource; }
            set { dataSource = value; }
        }

        private int count;

        /// <summary>
        /// 总记录数
        /// </summary>
        public int Count
        {
            get { return count; }
            set { count = value; }
        }

        /// <summary>
        /// 获取总页数
        /// </summary>
        /// <param name="count">总记录数</param>
        /// <param name="pagesize">页大小</param>
        /// <returns></returns>
        private int GetPageCount(int count, int pagesize)
        {
            if ((count % pagesize) == 0)
            {
                return count / pagesize;
            }
            else
            {
                return (count / pagesize) + 1;
            }
        }

        /// <summary>
        /// 总页数
        /// </summary>
        public int PageCount
        {
            get { return GetPageCount(this.Count, this.PageSize); }
        }

        /// <summary>
        /// 下一页
        /// </summary>
        public void PageDown()
        {
            if (this.CurrentPageIndex >= this.PageCount)
            {
                return;
            }
            else
            {
                this.CurrentPageIndex = this.CurrentPageIndex + 1;
            }
        }

        /// <summary>
        /// 上一页
        /// </summary>
        public void PageUp()
        {
            if (this.CurrentPageIndex <= 1)
            {
                return;
            }
            else
            {
                this.CurrentPageIndex = this.CurrentPageIndex - 1;
            }
        }

        /// <summary>
        /// 跳转到某页
        /// </summary>
        /// <param name="gotoPageIndex">页索引</param>
        public void PageTo(int gotoPageIndex)
        {
            if (gotoPageIndex >= 1 && gotoPageIndex < this.PageCount)
            {
                this.CurrentPageIndex = gotoPageIndex;
            }
        }

    }
}
